package com.firmail.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.firmail.entity.AppLog;
import com.firmail.entity.ExtendedParam;
import com.firmail.response.ResponseInfo;
import com.firmail.response.ResponseInfoList;
import com.firmail.service.AppLogService;

@RestController
public class FileController {

	@Autowired
	AppLogService appLogService;

	@RequestMapping(value = "file/logs", method = RequestMethod.POST)
	public ResponseInfo<ResponseInfoList<?>> logs(@RequestBody(required = false) String jsonInfo,
			HttpServletRequest request) {
		ResponseInfo<ResponseInfoList<?>> info = new ResponseInfo<>();
		ResponseInfoList<?> infoList = new ResponseInfoList<>();
		ExtendedParam param = new ExtendedParam();
		System.out.println("firmail-center:" + jsonInfo);
		try {
			if (!StringUtils.isEmpty(jsonInfo)) {
				JSONObject json = JSONObject.parseObject(jsonInfo);
				param.putAll(json);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		List<AppLog> list = appLogService.getLogs(param);
		if (list != null && list.size() > 0) {
			infoList.setTotal(list.size());
			infoList.setList(list);
		}
		info.setData(infoList);
		return info;
	}

}
